package com.thinkfit.jy.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.common.lib.bean.VersionBean;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.VersionActivity;

public class KeyboardPopupWindow extends PopupWindow implements View.OnClickListener , PopupWindow.OnDismissListener {

    private Context mContext;

    private View view,v1;
    ConstraintLayout clBg;
    TextView tv1,tv2,tv3,tv4,tv5,tv6,tv7,tv8,tv9,tv0;
    ImageView tvDel;

    StringBuilder sb = new StringBuilder();
    TextView sbText;

    public KeyboardPopupWindow(Context context, TextView textView){
        mContext = context;
        sbText = textView;
        sb.append(sbText.getText().toString());
        view = LayoutInflater.from(context).inflate(R.layout.popupwindow_keyboard, null);
        clBg = view.findViewById(R.id.cl_bg);
        v1 = view.findViewById(R.id.v1);
        tv1 = view.findViewById(R.id.tv_1);
        tv2 = view.findViewById(R.id.tv_2);
        tv3 = view.findViewById(R.id.tv_3);
        tv4 = view.findViewById(R.id.tv_4);
        tv5 = view.findViewById(R.id.tv_5);
        tv6 = view.findViewById(R.id.tv_6);
        tv7 = view.findViewById(R.id.tv_7);
        tv8 = view.findViewById(R.id.tv_8);
        tv9 = view.findViewById(R.id.tv_9);
        tv0 = view.findViewById(R.id.tv_0);
        tvDel = view.findViewById(R.id.tv_del);

        v1.setOnClickListener(this);
        clBg.setOnClickListener(this);

        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tv5.setOnClickListener(this);
        tv6.setOnClickListener(this);
        tv7.setOnClickListener(this);
        tv8.setOnClickListener(this);
        tv9.setOnClickListener(this);
        tv0.setOnClickListener(this);
        tvDel.setOnClickListener(this);


        this.setContentView(view);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();

        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#4D000000"));
//        ColorDrawable dw = new ColorDrawable(Color.parseColor("#ffffff"));
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(this);

    }

    public void show(Activity context) {
        showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cl_bg:
                dismiss();
                break;
            case R.id.tv_1:
                sb.append(1);
                sbText.setText(sb.toString());
                break;
            case R.id.tv_2:
                sb.append(2);
                sbText.setText(sb.toString());
                break;
            case R.id.tv_3:
                sb.append(3);
                sbText.setText(sb.toString());
                break;
            case R.id.tv_4:
                sb.append(4);
                sbText.setText(sb.toString());
                break;
            case R.id.tv_5:
                sb.append(5);
                sbText.setText(sb.toString());
                break;
            case R.id.tv_6:
                sb.append(6);
                sbText.setText(sb.toString());
                break;
            case R.id.tv_7:
                sb.append(7);
                sbText.setText(sb.toString());
                break;
            case R.id.tv_8:
                sb.append(8);
                sbText.setText(sb.toString());
                break;
            case R.id.tv_9:
                sb.append(9);
                sbText.setText(sb.toString());
                break;
            case R.id.tv_0:
                sb.append(0);
                sbText.setText(sb.toString());
                break;
            case R.id.tv_del:
                if(sb.length()>0){
                    sb.deleteCharAt(sb.length()-1);
                    sbText.setText(sb.toString());
                }
                break;

        }
    }

    @Override
    public void onDismiss() {
          dismiss();
    }
}
